local DateTimeUtils = {}


function DateTimeUtils.Format(sec,fmt,useShort)
    fmt = string.lower(fmt)
    local fmtSecInfo = {}
    fmtSecInfo["d"] = 86400 -- 1天的秒数
    fmtSecInfo["h"] = 3600 --1小时的秒数
    fmtSecInfo["m"] = 60 --一分钟的秒数
    fmtSecInfo["s"] = 0

    local startIdx = 1
    local parttern = "%l+" 
    local i,j = string.find( fmt,parttern,startIdx)
    local fmtChar = string.match(fmt,parttern,startIdx)
    local retFmt = string.sub(fmt,1,i-1)
    while j and fmtChar do
        startIdx = j + 1
        local k,m = string.find( fmt,parttern,startIdx)
        local nextStart = k and k - 1 or #fmt
        local f = string.sub(fmtChar,1,1)
        local fmtSec  = fmtSecInfo[f]
        if not fmtSec then
            return "时间格式="..fmt.." 无效"
        end

        local val
        if fmtSec > 0 then
            val = math.floor(sec/fmtSec)
            sec = sec%fmtSec
        else
            val = sec 
        end
        
        if val > 0 or not useShort then
            local valStr = tostring(val)
            if #fmtChar==2 and val < 10 then
                valStr = "0"..val
            end
            local tempFmtStr = string.sub(fmt,i,nextStart)
            retFmt = retFmt..string.gsub(tempFmtStr,parttern,valStr)
        end
        fmtChar = string.match(fmt,parttern,startIdx)

        i,j= k,m
    end
    return retFmt
end


local sec = 0 + 3600*3 + 60*3 + 26
local fmt = "剩余dd天hh时mm分s秒"
print("Format=",DateTimeUtils.Format(sec,fmt,false))
print("Format=",DateTimeUtils.Format(sec,fmt,true)) --等于0的不显示出来
fmt = "活动剩余：hh:mm:ss"
print("Format=",DateTimeUtils.Format(sec,fmt,true))
fmt = "hh-mm-ss"
print("Format=",DateTimeUtils.Format(sec,fmt))

local timeStamp = os.time()
local nowDate = os.date("*t",timeStamp)
--timeStamp = os.time(nowDate)
local taDayWday = nowDate.wday - 1
if taDayWday == 0 then
    taDayWday = 7
end
local toDaySec = os.time({year = nowDate.year, month = nowDate.month, day = nowDate.day, hour = 23,
minute = 59, second = 59})
local toDayLeftSec = toDaySec - timeStamp
local daySec = 24 * 60 * 60
local newTime = (7-taDayWday)*daySec + toDayLeftSec

print("timeStamp=",timeStamp,"taDayWday=",taDayWday)



fmt = "剩余dd天hh时mm分s秒"
print("Format=",DateTimeUtils.Format(newTime,fmt))



return DateTimeUtils




